<script>
	import websocket from '@/utils/msg/connectSocketInit.js'

	import {
		userBindingZfb
	} from '@/api/http/main/my/zfbPay.js'

	export default {
		//初始化时执行，且只执行一次
		onLaunch: function() {
			//console.log('App Launch')
			
			/* var list = [
			  { id: 1, name: "张三", age: 30 },
			  { id: 2, name: "李四", age: 25 },
			  { id: 3, name: "王五", age: 35 }
			];
			
			list.sort(function(a, b){
			  return b.age - a.age;
			});
			
			console.log(list) */

			// plus.push.getClientInfoAsync((info) => {
			// 	let cid = info["clientid"];
			// 	console.log(cid)
			// });

			// #ifdef APP-PLUS
			//点击通知消息时，执行事件
			plus.push.addEventListener("click", function(message) {
				//自定义内容获取
				let payload = message.payload
				try {
					//完成业务逻辑代码
					//uni.$showMsg('接受到消息')
					uni.uniPushRoute()

				} catch (e) {
					//TODO handle the exception
				}
			})

			//收到消息时，执行事件
			plus.push.addEventListener("receive", function(message) {
				//自定义内容获取
				let payload = message.payload
				try {
					//完成业务逻辑代码
					//uni.$showMsg('接受到消息')
					uni.uniPushRoute()
				} catch (e) {
					//TODO handle the exception
				}
			})

			// #endif

			this.getDevice()
		},
		onShow: function(options) {
			//开启app，链接websocket
			var that = this
			setTimeout(() => {
				that.connectSocketInit()
			}, 100);

			//console.log('App Show')
			//支付宝快捷绑定
			// 判断是否是从支付宝中跳转回来的逻辑
			if (options.referrerInfo && options.referrerInfo.appId === '2088800000000000') {
				// 是从支付宝中跳转回来的
				this.zfbLogin()
			} else {
				// 不是从支付宝中跳转回来的
			}

		},
		onHide: function() {
			//console.log('App Hide')
			//关闭app 断开websocket
			this.closeSocket()
		},
		methods: {
			//1，检查是否登录
			judgeLogin() {
				let token = uni.getStorageSync('token');
				if (token == null || token == '') {
					//清空缓存
					uni.clearStorageSync()
					//删除vuex的缓存
					//this.cleanStateFun()

					//不存在则跳转⾄登录页
					return uni.reLaunch({
						url: "/pages/login/login/login",
						success: () => {
							//plus.navigator.closeSplashscreen();
						}
					})
				} else {
					//存在则关闭启动页进⼊⾸页
					//plus.navigator.closeSplashscreen();
				}
			},
			//2，连接上websocket
			connectSocketInit() {
				//连接上websocket
				if (uni.getStorageSync('isVisitorLogin') && (uni.myWebsocket == undefined || uni.myWebsocket == '' || uni
						.myWebsocket == null)) {
					//console.log('用户模式！')
					websocket.methods.connectSocket('47.107.35.139')
				}

				// if () {
				// 	/* uni.myWebsocket.socketTask.close
				// 	uni.myWebsocket = '' */
				// 	websocket.methods.connectSocket('47.107.35.139')
				// }
			},
			//关闭websocket
			closeSocket() {
				//console.log('关闭了连接')
				uni.myWebsocket.socketTask.close('1')
				uni.myWebsocket = ''
				//设置历史消息查询次数
				uni.setStorageSync('queryStorageMsg', 50)
			},

			//3，unipush处理
			uniPushRoute() {
				//获取当前页面路由
				let curRoute = this.$mp.page.route;

				var msgRoute = "msg"
				var newMsgRoute = " msg"

				if (curRoute.indexOf(msgRoute) != -1 || curRoute.indexOf(newMsgRoute) != -1) {
					return
				} else {
					uni.navigateTo({
						url: '/pages/msg/home/home'
					})
				}

			},

			//4，getDevice
			getDevice() {
				//获取当前设备信息
				var deviceType = 'PC'

				// #ifdef APP-PLUS
				deviceType = 'APP'
				if (uni.getSystemInfoSync().platform == 'android') {
					//deviceType = 'android'
					console.log("运行在安卓手机上");
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					//deviceType = 'ios'
					console.log("运行在苹果手机上");
				} else {
					console.log('运行在开发工具上');
				}
				// #endif
				//console.log(deviceType)
				uni.$deviceType = deviceType
				//console.log(uni.$deviceType)
			},

			//5，支付宝快捷绑定
			zfbLogin() {
				// #ifdef APP-PLUS
				let args = plus.runtime.arguments;
				//console.log(args); //这里可以看到从后端拿过来的urlscheme
				if (args) {
					plus.runtime.arguments = null; //进入之后就把urlscheme清空要不然下一次oushow时还会执行
					// 处理args参数，如直达到某新页面等
					//通过code请求获取user_id
					var authCode = args.split("=")[1];
					//console.log(authCode)
					userBindingZfb(authCode).then(res => {
						if (res.data.pubInfo.code !== 0) {
							return uni.$showMsg(res.data.pubInfo.message);
						}
						//参数保存
						var busInfo = res.data.busInfo
						uni.$showMsg('绑定成功！');
						setTimeout(() => {
							uni.navigateTo({
								url: "/subpkg/main/my/assets/assets/assets"
							})
						}, 500)
					})
					//console.log(authCode)
				}
				// #endif
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>